﻿using _07_04_DBHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _07_DBHelper的使用
{
    public partial class FrmAdd : Form
    {
        public FrmAdd()
        {
            InitializeComponent();
        }

        private void BindPro()
        {
            DataTable dt = new DataTable();
            DBHelper.PrepareCmd("select * from ProfessionInfo");
            dt = DBHelper.ExecQuery();
            DataRow dr = dt.NewRow();
            dr["professionID"] = "0";
            dr["professionName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
            this.cmbName.DataSource = dt;
            this.cmbName.DisplayMember = "professionName";
            this.cmbName.ValueMember = "professionID";
        }


        private void FrmAdd_Load_1(object sender, EventArgs e)
        {
            BindPro();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == null || this.textBox2.Text == ""||this.textBox3.Text=="") {
               MessageBox.Show("必选项不可为空");
                return;
            }
            string sql = "insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID) " +
                "values(@StuID,@StuName,@StuAge,@StuSex,@StuHobby ,@ProfessionID)";
            //预处理Sql语句
            DBHelper.PrepareCmd(sql);
            //处理参数
            DBHelper.SetParamter("StuID", this.textBox1.Text);
            DBHelper.SetParamter("StuName", this.textBox2.Text);
            DBHelper.SetParamter("StuAge", this.textBox3.Text);
            string sex = "";
            if (this.checkBox1.Checked == true)
            {
                sex = this.checkBox1.Text;
            }
            if (this.checkBox2.Checked == true)
            {
                sex = this.checkBox2.Text;
            }
            DBHelper.SetParamter("StuSex", sex);
            string hobby = "";
            foreach (CheckBox item in this.panel2.Controls)
            {
                if (item.Checked)
                {
                    if (!hobby.Equals(""))
                    {
                        hobby += ",";
                    }
                    hobby += item.Text;
                }
            }
            DBHelper.SetParamter("StuHobby", hobby);
            DBHelper.SetParamter("ProfessionID", this.cmbName.SelectedValue.ToString());
            int count = DBHelper.ExecNonQuery();
            if (count == 1)
            {
                MessageBox.Show("新增成功");
                this.Close();
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                this.checkBox2.Checked = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked)
            {
                this.checkBox1.Checked = false;
            }
        }
    }
}
